const ModBusRtuMasterPlugin= require("../plugins/ModBusRtuMasterSimplePlugin.js");
M=require("ming_node")
const Utils=require("../utils/Utils.js")
MB=  new ModBusRtuMasterPlugin({com:"COM18",brand:115200})
class DdaEntry{
    classNode;
    isSingleton;
    instanceNum;
    firstId;
    eachSpace;
};
class DdaInfo
{
    ver;
    entryNum;
    entry=[];
};
ddaInfo=new DdaInfo();

async function testReadDda(){
    ddaInfo.ver=(await MB.read(0,1))[0]
    ddaInfo.entryNum=(await MB.read(1,1))[0]
    for (let i=0;i<ddaInfo.entryNum;i++){
        let entryItem=new DdaEntry();
        let classNodeBuf=await MB.read(2+i*12,8);
        let classNodeStrBuf=[];
        for (let j=0;j<classNodeBuf.length;j++){
            if(classNodeBuf[j]>>8){
                classNodeStrBuf[2*j]=classNodeBuf[j]>>8;
            }else {
                break;
            }
            if(classNodeBuf[j]&0xff){
                classNodeStrBuf[2*j+1]=classNodeBuf[j]&0xff;
            }else {
                break;
            }
        }
        entryItem.classNode=String.fromCharCode.apply(String, classNodeStrBuf);
        entryItem.isSingleton= (await MB.read(2+i*12+8,1))[0]
        entryItem.instanceNum=(await MB.read(2+i*12+9,1))[0]
        entryItem.firstId=(await MB.read(2+i*12+10,1))[0]
        entryItem.eachSpace=(await MB.read(2+i*12+11,1))[0]
        entryItem.isSingleton=Utils.hexStringByWord(entryItem.isSingleton)
        entryItem.instanceNum=Utils.hexStringByWord(entryItem.instanceNum)
        entryItem.firstId=Utils.hexStringByWord(entryItem.firstId)
        entryItem.eachSpace=Utils.hexStringByWord(entryItem.eachSpace)
        ddaInfo.entry[i]=entryItem;
    }
    console.log(ddaInfo);
    process.exit()
}

async function testReadDrawer(){
     a=(await MB.read(0x500,8)).map(u=>u.toString(16))
     console.log(a)
    process.exit()
}
async function testWriteProductModel(){
    a=(await MB.write(0x400,[0x1234,0x2567]))
    console.log(a)
    process.exit()
}

async function main(){
    await testReadDda()
    //await testReadDrawer()
    //await testWriteProductModel()
    process.exit()
}

main()